package org.nhindirect.stagent;

import org.nhindirect.stagent.ProtocolException.ProtocolError;

public class NHINDMessage extends Message 
{
    NHINDAgent m_agent;
    NHINDAddress m_sender;
    
    NHINDMessage()
    {
    }
    
    public NHINDAgent getAgent()
    {
    	return this.m_agent;
    }

    public void setAgent(NHINDAgent value)
    {
        this.m_agent = value;
    }
            
    @Override
    public Header getFrom()
    {
    	return super.getFrom();
    }
        
    public void setFrom(Header value) 
    {
        super.setFrom(value);
        this.m_sender = null;
    }

    
    public NHINDAddress getSender()
    {
	    if (this.m_sender == null)
	    {
	        this.m_sender = this.toNHINDAddress(this.getFrom());
	    }
	    
	    return this.m_sender;
    }
    
    public void validate()
    {
        if (this.getParsedTo() == null)
        {
            throw new ProtocolException(ProtocolError.MissingTo);
        }

        if (this.getParsedFrom() == null)
        {
            throw new ProtocolException(ProtocolError.MissingFrom);
        }
    }        
    
    NHINDAddress toNHINDAddress(Header header)
    {
        if (header == null)
        {
            return null;
        }

        return new NHINDAddress(header.getValue());
    }

}
